﻿using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using SysCommon.Entity.TMSIM.WS.Stakeout;
using System.Drawing;

//using GISCommon.Business;

namespace TMSIM.Client.BizGIS.Business
{
    /// <summary>
    /// 该类主要为历史回放服务
    /// </summary>
    public class Geometry:GISCommon.Business.GeometryTools
    {
        public Geometry()
        {
        }
        public Geometry(AxMapControl inMapControl)
        {
            pMapControl = inMapControl;
        }

        
        private string carPictureFile = "";
        public string CarPictureFile
        {
            get { return carPictureFile; }
            set { carPictureFile = value; }
        }

        private IPictureMarkerSymbol pictureMarkerSymbol = null;
        
        public void Initial()
        {
            try
            {
                //设置车辆图标
                pictureMarkerSymbol = new PictureMarkerSymbolClass();
                esriIPictureType pPictureType = esriIPictureType.esriIPictureBitmap;
                pictureMarkerSymbol.CreateMarkerSymbolFromFile(pPictureType, carPictureFile);
                pictureMarkerSymbol.Size = 18;
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Geometry的Initial出错！", ex);
            }
        }

        public void Initial(string pictureFile)
        {
            try
            {
                carPictureFile = pictureFile;

                //设置车辆图标
               
            
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Geometry的Initial出错！", ex);
            }

        }
        /// <summary>
        /// 描述：在地图上画车辆
        /// </summary>
        /// <param name="ety">历史回放实体</param>
        /// <param name="inMapControl">地图控件</param>
        /// <returns>车辆元素</returns>
        public IElement DrawCar(CarHistory_Ety ety, ESRI.ArcGIS.Controls.AxMapControl inMapControl)
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用DrawCar函数！");
                //生成点对象
                stdole.IPictureDisp iconCar = null;
                Image img = Image.FromFile(carPictureFile);
                iconCar = ImageConverter2.ImageToIPicture(img);
                ESRI.ArcGIS.Geometry.IPoint pPoint = new ESRI.ArcGIS.Geometry.PointClass();
                pPoint.PutCoords(ety.LONGITUDE, ety.LATITUDE);

                ISymbol pSymbol;

                IElement pElement;
                IMarkerElement pMarkerElement;
                IGraphicsContainer pGraCon;
                IMap pMap = inMapControl.ActiveView.FocusMap;

                pGraCon = (IGraphicsContainer)pMap;
                
                double dblAngle = ety.HEADING;

                IRgbColor pRgbColor = (IRgbColor)new RgbColorClass();
                pRgbColor.Red = 255;

                ISimpleTextSymbol pTextSymbol = (ISimpleTextSymbol)new TextSymbolClass();
                //IMarkerTextBackground pMarkerTextBG = (IMarkerTextBackground)new MarkerTextBackgroundClass();
                pTextSymbol.Color = pRgbColor;
                pTextSymbol.Size = 12;
                pTextSymbol.YOffset = 8;
                //pMarkerTextBG.Symbol =(IMarkerSymbol) pTextSymbol;


                 pictureMarkerSymbol = new PictureMarkerSymbolClass();
                esriIPictureType pPictureType = esriIPictureType.esriIPictureBitmap;
                pictureMarkerSymbol.Size = 18;
             //pictureMarkerSymbol.CreateMarkerSymbolFromFile(pPictureType, carPictureFile);
                pictureMarkerSymbol.Angle = dblAngle;
                pictureMarkerSymbol.Picture = iconCar;
                pSymbol = (ISymbol)pictureMarkerSymbol;

                //创建新的PictureMarkerElement对象
                pMarkerElement = (IMarkerElement)new MarkerElementClass();
                pMarkerElement.Symbol = (IMarkerSymbol)pSymbol;

                pElement = (IElement)pMarkerElement;
                pElement.Geometry = pPoint;

                pGraCon.AddElement(pElement, 0);

                //AddTextBackground(pPoint);
                //inMapControl.MapScale = 3696;
                inMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, pElement, null);

                SysCommon.Global.mainLog.Debug("调用DrawCar函数结束！");
                return pElement;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DrawCar函数出错！", ex);
                return null;
            }
        }

        /// <summary>
        /// 描述：在地图上画车辆
        /// </summary>
        /// <param name="ety">历史回放实体</param>
        /// <returns>车辆元素</returns>
        public IElement DrawCar(CarHistory_Ety ety)
        {

            try
            {
                SysCommon.Global.mainLog.Debug("开始调用DrawCar函数！");

                ESRI.ArcGIS.Geometry.IPoint pPoint = new ESRI.ArcGIS.Geometry.PointClass();
                pPoint.PutCoords(ety.LONGITUDE, ety.LATITUDE);

                string strCarPlate = ety.CARPLATE;
                ISymbol pSymbol;

                IElement pElement;
                IMarkerElement pMarkerElement;
                IGraphicsContainer pGraCon;
                IMap pMap = pMapControl.ActiveView.FocusMap;

                pGraCon = (IGraphicsContainer)pMap;
                double dblAngle = ety.HEADING;

                //设置车辆符号
                IRgbColor pRgbColor = (IRgbColor)new RgbColorClass();
                pRgbColor.Red = 255;

                ISimpleTextSymbol pTextSymbol = (ISimpleTextSymbol)new TextSymbolClass();
                //IMarkerTextBackground pMarkerTextBG = (IMarkerTextBackground)new MarkerTextBackgroundClass();
                pTextSymbol.Color = pRgbColor;
                pTextSymbol.Size = 12;
                pTextSymbol.YOffset = 8;
                //pMarkerTextBG.Symbol =(IMarkerSymbol) pTextSymbol;

                pictureMarkerSymbol.Angle = dblAngle;
                pSymbol = (ISymbol)pictureMarkerSymbol;

                //创建新的PictureMarkerElement对象
                pMarkerElement = (IMarkerElement)new MarkerElementClass();
                pMarkerElement.Symbol = (IMarkerSymbol)pSymbol;

                pElement = (IElement)pMarkerElement;
                pElement.Geometry = pPoint;

                pGraCon.AddElement(pElement, 0);

                //AddTextBackground(pPoint);

                pMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, pElement, null);

                SysCommon.Global.mainLog.Debug("调用DrawCar函数结束！");
                return pElement;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DrawCar函数出错！", ex);
                return null;

            }
        }

        /// <summary>
        /// 描述：更新车辆位置信息
        /// </summary>
        /// <param name="inElement">原车辆位置元素</param>
        /// <param name="ety">历史回放实体</param>
        /// <param name="inMapControl">地图控件</param>
        /// <returns>更新后的车辆元素</returns>
        public IElement UpdateCarPosition(IElement inElement, CarHistory_Ety ety, ESRI.ArcGIS.Controls.AxMapControl inMapControl)
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用UpdateCarPosition函数！");
                ESRI.ArcGIS.Geometry.IPoint pPoint = new ESRI.ArcGIS.Geometry.PointClass();
                pPoint.PutCoords(ety.LONGITUDE, ety.LATITUDE);
                IMarkerElement markerElement = (IMarkerElement)inElement;

                //inMapControl.ActiveView.GraphicsContainer.DeleteElement(inElement);
                //inMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, inElement, null);

                IMarkerSymbol symbol = markerElement.Symbol;
                symbol.Angle = ety.HEADING;
                markerElement.Symbol = symbol;
                IElement pElement = (IElement)markerElement;
                pElement.Geometry = pPoint;

                inMapControl.ActiveView.GraphicsContainer.UpdateElement(pElement);
                //inMapControl.ActiveView.GraphicsContainer.AddElement(pElement,0);
                MoveToVisibleScreen(pPoint, inMapControl);
                //inMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, inElement, null);
                inMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
                SysCommon.Global.mainLog.Debug("调用UpdateCarPosition函数结束！");
                return pElement;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用UpdateCarPosition函数出错！", ex);
                return null;
            }
        }

        /// <summary>
        /// 描述：更新车辆位置信息
        /// </summary>
        /// <param name="inElement">原车辆位置元素</param>
        /// <param name="ety">历史回放实体</param>
        /// <returns>更新后的车辆元素</returns>
        public IElement UpdateCarPosition(IElement inElement, CarHistory_Ety ety)
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用UpdateCarPosition函数！");
                ESRI.ArcGIS.Geometry.IPoint pPoint = new ESRI.ArcGIS.Geometry.PointClass();
                pPoint.PutCoords(ety.LONGITUDE, ety.LATITUDE);
                IMarkerElement markerElement = (IMarkerElement)inElement;

                //pMapControl.ActiveView.GraphicsContainer.DeleteElement(inElement);
                //pMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, inElement, null);

                IMarkerSymbol symbol = markerElement.Symbol;
                symbol.Angle = ety.HEADING;
                markerElement.Symbol = symbol;
                IElement pElement = (IElement)markerElement;
                pElement.Geometry = pPoint;

                pMapControl.ActiveView.GraphicsContainer.UpdateElement(pElement);
                //pMapControl.ActiveView.GraphicsContainer.AddElement(pElement, 0);
                MoveToVisibleScreen(pPoint, pMapControl);
                pMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
                SysCommon.Global.mainLog.Debug("调用UpdateCarPosition函数结束！");
                return pElement;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用UpdateCarPosition函数出错！", ex);
                return null;
            }
        }

        private static void MoveToVisibleScreen(ESRI.ArcGIS.Geometry.IPoint point, ESRI.ArcGIS.Controls.AxMapControl mapCtrl)
        {
            try
            {
                ////获取当前屏幕中心位置
                int x = mapCtrl.Width/2;
                int y = mapCtrl.Height/2;
                ESRI.ArcGIS.Geometry.IPoint pt = mapCtrl.ToMapPoint(x, y);

                ////计算偏移量

                double dx = 0;
                double dy = 0;
                ESRI.ArcGIS.Geometry.IEnvelope envelope = mapCtrl.Extent;
                envelope.Expand(0.9, 0.9, true);
                //double ration = 0.0000001* mapCtrl.MapScale;
                double xmin = envelope.XMin; // mapCtrl.Extent.XMin;
                double xmax = envelope.XMax; //mapCtrl.Extent.XMax;
                double ymin = envelope.YMin; // mapCtrl.Extent.YMin;
                double ymax = envelope.YMax; // mapCtrl.Extent.YMax;
                double x0 = point.X;
                double y0 = point.Y;

                if (x0 <= xmin)
                    dx = 8*(x0 - pt.X)/9;
                if (x0 >= xmax)
                    dx = 8*(x0 - pt.X)/9;
                if (y0 <= ymin)
                    dy = 8*(y0 - pt.Y)/9;
                if (y0 >= ymax)
                    dy = 8*(y0 - pt.Y)/9;

                //新中心点位置
                if (dx == 0 && dy == 0)
                    return;
                else
                {
                    double dxm = mapCtrl.Extent.XMax - mapCtrl.Extent.XMin;
                    double dym = mapCtrl.Extent.YMax - mapCtrl.Extent.YMin;
                    //dxm = Math.Abs(dxm);
                    //dym = Math.Abs(dym);
                    if ((Math.Abs(dx) > dxm) || (Math.Abs(dy) > dym))
                    {
                        //Console.WriteLine("dx={0}\tdy={1}\ndxm={2}\tdym={3}", Math.Abs(dx), Math.Abs(dy), dxm, dym);
                        mapCtrl.CenterAt(point);
                        mapCtrl.ActiveView.Refresh();
                        return;
                    }

                    if (dx != 0)
                    {
                        point.X = point.X + dx;
                    }
                    if (dy != 0)
                    {
                        point.Y = point.Y + dy;
                    }
                }

                //移动视图
                mapCtrl.CenterAt(point);
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Geometry的MoveToVisibleScreen出错！", ex);
            }
        }
        private AxMapControl pMapControl = null;
        public AxMapControl MAPCONTROL
        {
            get { return pMapControl; }
            set { pMapControl = value; }
        }
    }
}
